//搜索关键字查询
var systemAddUserKeywordFunction = function(){
    $.ajax({
        url:"htmlWeb/keyword/allList",
        type:'POST',
        dataType:"json",
        success:function(result){
            var tem = "";
            $.each( result, function(index, content){
                if(content.openType == null){
                    tem += "<label><input type='checkbox' name='userBox' class='selectDiv' value='"+content.id+"'/>"+content.name+"</label>";
                }else {
                    tem += "<label class='cur'><input type='checkbox' name='userBox' class='selectDiv' checked='checked' value='"+content.id+"'/>"+content.name+"</label>";
                }
            });
            $("#systemAddUserKeyword").empty();
            $("#systemAddUserKeyword").append(tem);

            //动态加class
            $(".userDiv ul li p label").click(function(){
                $(this).toggleClass("on");
            });
        }
    });
}();

//根据市查询所有区
var countyFunction = function(city){
    $.ajax({
        url:"cityWeb/findAllAreaByCity.json",
        type:'POST',
        dataType:"json",
        data:{
            city:city
        },
        success:function(result){
            var tem = "<option value=''>区域</option>";
            $.each( result.byOpenCityList, function(index, content){
                tem += "<option value='"+content.area+"'>"+content.area+"</option>";
            });
            $("#county").empty();
            $("#county").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

//根据省份查询所有市
var cityFunction = function(province){
    $.ajax({
        url:"cityWeb/findAllCityByProvince.json",
        type:'POST',
        dataType:"json",
        data:{
            province:province
        },
        success:function(result){
            var tem = "<option value=''>城市</option>";
            $.each( result.byOpenCityList, function(index, content){
                tem += "<option value='"+content.city+"'>"+content.city+"</option>";
            });
            $("#city").empty();
            $("#city").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

//获取开通省份
var provinceFunction = function(){
    $.ajax({
        url:"cityWeb/findAllProvince.json",
        type:'GET',
        dataType:"json",
        success:function(result){
            var tem = "<option value=''>省份</option>";
            $.each( result.byOpenCityList, function(index, content){
                tem += "<option value='"+content.province+"'>"+content.province+"</option>";
            });
            $("#province").empty();
            $("#province").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}();

function provinceChange(){
    var options=$("#province option:selected");
    cityFunction(options.val());
}

function cityChange () {
    var options=$("#city option:selected");
    countyFunction(options.val());
}

function areaChange(){
    var options=$("#county option:selected");
    $.ajax({
        url:"usersWeb/findAllUserByAreaType.json",
        type:'POST',
        dataType:"json",
        data:{
            area:options.val()
        },
        success:function(result){
            var tem = "<option value=''>所属代理商</option>";
            $.each( result.agents, function(index, content){
                tem += "<option value='"+content.id+"'>"+content.username+"</option>";
            });
            $("#addUserAgent").empty();
            $("#addUserAgent").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

var areaChangeFrist = function(){
    $.ajax({
        url:"usersWeb/findAllUserByAreaType.json",
        type:'POST',
        dataType:"json",
        data:{
            area:loginArea
        },
        success:function(result){
            var tem = "<option value=''>所属代理商</option>";
            $.each( result.agents, function(index, content){
                tem += "<option value='"+content.id+"'>"+content.username+"</option>";
            });
            $("#addUserAgent").empty();
            $("#addUserAgent").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}();

//权限角色
var dictionFunction = function(){
    $.ajax({
        url:"systemRole/findAllRoleByTypePage.json",
        type:'GET',
        dataType:"json",
        data:{
            roleType:1,page:1,pageNum:1000
        },
        success:function(result){
            var tem = "<option value='3'>请选择权限角色</option>";
            $.each( result.roleList, function(index, content){
                if(content.code != '001'){
                    tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
                }
            });
            $("#diction").empty();
            $("#diction").append(tem);
        }
    });
}();

//职业角色
var occupationFunction = function(){
    $.ajax({
        url:"systemRole/findAllRoleByTypePage.json",
        type:'GET',
        dataType:"json",
        data:{
            roleType:2,page:1,pageNum:1000
        },
        success:function(result){
            var tem = "<option value='10'>请选择职业角色</option>";
            $.each( result.roleList, function(index, content){
                tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
            });
            $("#occupation").empty();
            $("#occupation").append(tem);

        }
    });
}();

//佣金角色
var commissionFunction = function(){
    $.ajax({
        url:"systemRole/findAllRoleByTypePage.json",
        type:'GET',
        dataType:"json",
        data:{
            roleType:3,page:1,pageNum:1000
        },
        success:function(result){
            var tem = "<option value='16'>请选择佣金角色</option>";
            $.each( result.roleList, function(index, content){
                tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
            });
            $("#commission").empty();
            $("#commission").append(tem);

        }
    });
}();

//信誉角色
var credibilityFunction = function(){
    $.ajax({
        url:"systemRole/findAllRoleByTypePage.json",
        type:'GET',
        dataType:"json",
        data:{
            roleType:4,page:1,pageNum:1000
        },
        success:function(result){
            var tem = "<option value='17'>请选择信誉角色</option>";
            $.each( result.roleList, function(index, content){
                tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
            });
            $("#credibility").empty();
            $("#credibility").append(tem);

        }
    });
}();

//重置
var cancelFunction = function(){
    navPills('systemUserControlHtml',2);
}

//表单提交
var saveFunction = function(){
    if($('input[name="userBox"]:checked').size() > 0){
        var userBox = $('input[name="userBox"]:checked');
        var userChannels = '';
        $.each(userBox, function(index, content){
            userChannels += content.defaultValue + ',';
        });
        userChannels = userChannels.substr(0,userChannels.length-1);
        $("input[name='userChannel']").val(userChannels);
    }
    $("#systemAddUserForm").ajaxForm({
        dataType: 'json',
        success: function (data) {
            if(data.r == 'OK'){
                hintsPValueFucntion("操作成功");
                navPills('systemUserControlHtml',2);
            }else {
                hintsPValueFucntion("操作失败");
            }
        },
        error:function () {
            hintsPValueFucntion("请求失败");
        }
    });
};

var closeDivFunction = function (){
    $(".deskDiv").fadeOut();
    $(".roleDele").fadeOut();
    $(".roleNew").fadeOut();
    $(".hints").fadeOut();
    $(".authority").fadeOut();
    $(".modifyOrder").fadeOut();
    $(".hintsDiv").fadeOut();
    $(".teamDiv").fadeOut();
    $(".refundDiv").fadeOut();
    $(".addNote").fadeOut();
    $(".evaluation").fadeOut();
}

//关闭
$(".cancel,.confirm").click(function(){
    closeDivFunction();
});

//友情提示
var hintsPValueFucntion = function(v){
    $("#hintsPValue").html(v);
    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
}